#!/bin/bash

readonly PACMAN_GNUPG_BACKUP_DIR=/etc/pacman.d.gnupg-backup-$(date '+%s')


mv /etc/pacman.d/gnupg $PACMAN_GNUPG_BACKUP_DIR 2> /dev/null
if   sudo pacman -Scc                                    &&
     sudo pacman -Syy --noconfirm   archlinux-keyring     \
                                    archlinux32-keyring   \
                                    archlinuxarm-keyring  \
                                    parabola-keyring     &&
     sudo pacman-key --init                              &&
     sudo pacman-key --populate     archlinux             \
                                    archlinux32           \
                                    archlinuxarm          \
                                    parabola             &&
     sudo pacman-key --refresh-keys
then echo "the parabola keyring has been refreshed"
     rm -rf $PACMAN_GNUPG_BACKUP_DIR
else echo "there was a problem refreshing the parabola keyring"
     if   [ -d "$PACMAN_GNUPG_BACKUP_DIR" ]
     then rm -rf /etc/pacman.d/gnupg 2> /dev/null
          mv $PACMAN_GNUPG_BACKUP_DIR /etc/pacman.d/gnupg
     else echo "/etc/pacman.d/gnupg is locked"
     fi
fi
